package jmotioncapture.camera;

import jmotioncapture.geometry2D.Point;
import jmotioncapture.geometry2D.PointComparable;
import jmotioncapture.calculation.FrameCalculation;

/**
 * 
 * @author Kenneth
 * @since 1.0.0
 */
public class Screen {

	/**
	 * @since 1.0.0
	 */
	private Point topLeftPoint;

	/**
	 * @since 1.0.0
	 */
	private Point bottomLeftPoint;

	/**
	 * @since 1.0.0
	 */
	private Point topRightPoint;

	/**
	 * @since 1.0.0
	 */
	private Point bottomRightPoint;

	/**
	 * @since 1.0.0
	 */
	private boolean status = false;

	/**
	 * @since 1.0.0
	 */
	public Screen() {

	}

	/**
	 * 
	 * @param frameSet
	 * @param frame
	 * @return
	 * @since 1.0.0
	 */
	public void init(FrameSet frameSet, Frame frame) {

		Frame frameStaticPoint = new Frame();

		// All the points must be static point
		for (int i = 0; i < frame.getNumberOfPoints(); i++)
			if (FrameCalculation.isStaticPoint(frame.getPoint(i), frameSet))
				frameStaticPoint.addPoint(frame.getPoint(i));

		if (frameStaticPoint.getNumberOfPoints() == 4) {
			setPoints(frameStaticPoint);
			this.setStatus(true);
		} else
			this.setStatus(false);
	}

	/**
	 * 
	 * @param frame
	 * @since 1.0.0
	 */
	private void setPoints(Frame frame) {
		PointComparable[] pointSortedByX = frame.sortPointsByX();

		if (pointSortedByX[0].getY() < pointSortedByX[1].getY()) {
			this.setTopLeftPoint(pointSortedByX[0]);
			this.setBottomLeftPoint(pointSortedByX[1]);
		} else {
			this.setTopLeftPoint(pointSortedByX[1]);
			this.setBottomLeftPoint(pointSortedByX[0]);
		}

		if (pointSortedByX[2].getY() < pointSortedByX[3].getY()) {
			this.setTopRightPoint(pointSortedByX[2]);
			this.setBottomRightPoint(pointSortedByX[3]);
		} else {
			this.setTopRightPoint(pointSortedByX[3]);
			this.setBottomRightPoint(pointSortedByX[2]);
		}
	}

	/**
	 * 
	 * @return
	 * @since 1.0.0
	 */
	public Point getBottomLeftPoint() {
		return bottomLeftPoint;
	}

	/**
	 * 
	 * @return
	 * @since 1.0.0
	 */
	public Point getBottomRightPoint() {
		return bottomRightPoint;
	}

	/**
	 * 
	 * @return
	 * @since 1.0.0
	 */
	public Point getTopLeftPoint() {
		return topLeftPoint;
	}

	/**
	 * 
	 * @return
	 * @since 1.0.0
	 */
	public Point getTopRightPoint() {
		return topRightPoint;
	}

	/**
	 * 
	 * @param bottomLeftPoint
	 * @since 1.0.0
	 */
	public void setBottomLeftPoint(Point bottomLeftPoint) {
		this.bottomLeftPoint = bottomLeftPoint;
	}

	/**
	 * 
	 * @param bottomRightPoint
	 * @since 1.0.0
	 */
	public void setBottomRightPoint(Point bottomRightPoint) {
		this.bottomRightPoint = bottomRightPoint;
	}

	/**
	 * 
	 * @param topLeftPoint
	 * @since 1.0.0
	 */
	public void setTopLeftPoint(Point topLeftPoint) {
		this.topLeftPoint = topLeftPoint;
	}

	/**
	 * 
	 * @param topRightPoint
	 * @since 1.0.0
	 */
	public void setTopRightPoint(Point topRightPoint) {
		this.topRightPoint = topRightPoint;
	}

	@Override
	public String toString() {
		return this.topLeftPoint.toString() + ";"
				+ this.topRightPoint.toString() + ";"
				+ this.bottomLeftPoint.toString() + ";"
				+ this.bottomRightPoint.toString();
	}

	public boolean getStatus() {
		return status;
	}

	public void setStatus(boolean status) {
		this.status = status;
	}
}
